package com.server.web;

import cn.hutool.core.util.ArrayUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import lombok.SneakyThrows;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Classname FengRui
 * @Description
 * @Date 2022-12-03 13:47
 */
@SpringBootTest
public class Text2 {

    private static String examineUrl="http://pinggu-oss.oss-cn-hangzhou.aliyuncs.com";

    private static String endpoint= "http://oss-cn-hangzhou.aliyuncs.com";

    private static String accessKeyId = "LTAI5tQQcPG1PZHt1rrJ85ca";

    private static String accessKeySecret = "7Lj7dv1a2IIKpyasf9Y0WcxmUokaLH";

    private static String bucketName = "pinggu-oss";

    @Test
    public void main() {
        File folder = new File("D:\\HengXiangShouImage\\2020年其他的图片");
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            if (file.isFile()) {
                System.out.println(file.getName());
                FileItem fileItem = getMultipartFile(file,file.getName());
                MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
                uploadFile(multipartFile,file.getName());
            }
        }
    }

    @SneakyThrows
    public static String uploadFile(MultipartFile multipartFile, String fileName){

        if (multipartFile == null){
            throw new RuntimeException("multipartFile cat`t be null");
        }
        String[] split = multipartFile.getOriginalFilename().split("\\.");
        if (ArrayUtil.isAllEmpty(split)){
            throw new RuntimeException("fileName cat`t be null");
        }
        String objectName = "Public/uploads/files/"+fileName;
        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, multipartFile.getInputStream());
            ossClient.putObject(putObjectRequest);
            return examineUrl+"/"+objectName;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
    private static FileItem getMultipartFile(File file, String fieldName){
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(fieldName, "text/plain", true, file.getName());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return item;
    }

    /**
     * List平均分成多个子List
     *
     */



}
